begintownscript;
variables;

short lastskelspawn, choice, timeleft;

body;

beginstate INIT_STATE;
	if (get_flag(2,20) == 1)	{
		set_terrain(24,29,0);	}
	if (get_flag(2,22) == 6)	{
		set_terrain(30,25,0);
		set_terrain(23,28,267);
		set_terrain(25,28,267);
		set_terrain(26,27,267);
		set_terrain(26,25,267);
		set_terrain(22,25,267);
		set_terrain(22,27,267);	}
	if (get_flag(2,21) == 1)	{
		set_terrain(27,43,0);
		set_terrain(27,40,0);
		set_terrain(26,41,0);
		set_terrain(24,41,0);
		set_terrain(22,41,0);
		set_terrain(21,40,0);
		set_terrain(21,43,0);	}

	add_range_to_group(7,13,1);

	set_level(14,12);
	set_level(15,12);
break;

beginstate START_STATE;
	if ((get_flag(2,24) == 1) && (get_flag(2,25) == 0))	{
		lastskelspawn = lastskelspawn + 1;
		if (lastskelspawn == 17)	{
			lastskelspawn = 0;
			spawn_creature(18);
			spawn_creature(19);
			spawn_creature(20);
			spawn_creature(21);
			put_effect_on_char(18,2,4,0);
			put_effect_on_char(19,2,4,0);
			put_effect_on_char(20,2,4,0);
			put_effect_on_char(21,2,4,0);
			run_animation();
			alert_char(18);
			alert_char(19);
			alert_char(20);
			alert_char(21);	}	}
	if (get_flag(2,25) == 1)	{
		if (timeleft == 0)	{
			message_dialog("Too late: the walls crash down around you with a great rumble as boulders rain down upon your head.  What a horrible place to die.","");
			kill_char(1000,2,0);	}
		else	{
			timeleft = timeleft - 1;	}	}
break;

beginstate EXIT_STATE;
break;

beginstate 10;
	move_to_new_town(6,7,5);
break;

beginstate 11;
	if (get_flag(2,19) == 0)	{
		message_dialog("A chilly blast of air swirls by you as you step into the musty room.  There are several graves in the room, but there does not appear to be anything moving in the earth before them.","In the center of the room is a large stone pad, slightly sticking out of the ground, almost as if it were a button.");
		set_flag(2,19,1);	}
break;

beginstate 12;
	if (get_flag(2,20) == 0)	{
		message_dialog("You step on the platform and it shifts down to be level with the floor.  A rumble is heard as a part of the south wall moves away.","");
		set_flag(2,20,1);
		set_terrain(24,29,0);	}
break;

beginstate 13;
	if (get_flag(2,21) == 0)	{
		force_view_center(24,40);
		force_instant_terrain_redraw();
		pause(15);
		spawn_creature(6);
		put_boom_on_char(6,2,0);
		run_animation_sound(10);
		pause(25);
		text_bubble_on_char(6,"You again?!");
		force_instant_terrain_redraw();
		pause(25);
		text_bubble_on_char(6,"");
		text_bubble_on_char(6,"Why aren't you dead yet?");
		force_instant_terrain_redraw();
		pause(25);
		text_bubble_on_char(6,"");
		text_bubble_on_char(6,"Grr... take this, puny mortal people!");
		force_instant_terrain_redraw();
		pause(10);
		set_character_pose(6,1);
		force_instant_terrain_redraw();
		pause(5);
		text_bubble_on_char(6,"");
		force_instant_terrain_redraw();
		put_boom_on_char(6,4,0);
		run_animation_sound(54);
		pause(5);
		activate_hidden_group(2);
		put_effect_on_char(7,2,4,0);
		put_effect_on_char(8,2,4,0);
		put_effect_on_char(9,2,4,0);
		put_effect_on_char(10,2,4,0);
		put_effect_on_char(11,2,4,0);
		put_effect_on_char(12,2,4,0);
		put_effect_on_char(13,2,4,0);
		set_terrain(27,43,0);
		set_terrain(27,40,0);
		set_terrain(26,41,0);
		set_terrain(24,41,0);
		set_terrain(22,41,0);
		set_terrain(21,40,0);
		set_terrain(21,43,0);
		run_animation();
		pause(2);
		set_character_pose(6,2);
		force_instant_terrain_redraw();
		pause(5);
		text_bubble_on_char(6,"Hah! What do you say to that?");
		force_instant_terrain_redraw();		
		pause(25);
		text_bubble_on_char(6,"");
		text_bubble_on_char(6,"Now, if you'll excuse me, I must go...");
		force_instant_terrain_redraw();	
		pause(25);
		text_bubble_on_char(6,"");
		text_bubble_on_char(6,"Evil plans don't execute themselves!");
		force_instant_terrain_redraw();
		pause(25);
		put_boom_on_char(6,2,0);
		erase_char(6);
		run_animation_sound(10);
		force_view_center(24,35);
		force_instant_terrain_redraw();
		alert_char(1001);
		block_entry(1);
		set_flag(2,21,1);
		set_flag(3,6,1);	}
break;

beginstate 14;
	message_dialog("The inscription reads:  _I shall serve the Guild and its masters for eternity._","");
	if ((get_flag(2,22) == 0) && (get_flag(2,21) == 1))	{
		set_flag(2,22,1);	
		set_terrain(23,28,267);	}
	else
		set_state_continue(15);
break;

beginstate 15;
	if (get_flag(2,22) != 6)	{
		set_terrain(23,28,266);
		set_terrain(25,28,266);
		set_terrain(26,27,266);
		set_terrain(26,25,266);
		set_terrain(22,25,266);
		set_terrain(22,27,266);
		set_flag(2,22,0);	}
break;

beginstate 16;
	message_dialog("The inscription reads:  _Never shall I renounce this Oath or be eternal damnation upon me._","");
	if ((get_flag(2,22) == 1) && (get_flag(2,21) == 1))	{
		set_flag(2,22,2);	
		set_terrain(25,28,267);	}
	else
		set_state_continue(15);
break;

beginstate 17;
	message_dialog("The inscription reads:  _Always I will uphold the code of the Guild._","");
	if ((get_flag(2,22) == 2) && (get_flag(2,21) == 1))	{
		set_flag(2,22,3);	
		set_terrain(26,27,267);	}
	else
		set_state_continue(15);
break;

beginstate 18;
	message_dialog("The inscription reads:  _May the darkness envelop me as I become one with death;  merging the essences of war, chaos, and pain into my being._","");
	if ((get_flag(2,22) == 3) && (get_flag(2,21) == 1))	{
		set_flag(2,22,4);	
		set_terrain(26,25,267);	}
	else
		set_state_continue(15);
break;

beginstate 19;
	message_dialog("The inscription reads:  _With the power granted me by these forces I shall strive to mend the plague that is life._","");
	if ((get_flag(2,22) == 4) && (get_flag(2,21) == 1))	{
		set_flag(2,22,5);	
		set_terrain(22,25,267);	}
	else
		set_state_continue(15);
break;

beginstate 20;
	message_dialog("The inscription reads:  _In death we shall rejoice._","");
	if ((get_flag(2,22) == 5) && (get_flag(2,21) == 1))	{
		set_flag(2,22,6);	
		set_terrain(22,27,267);
		set_terrain(30,25,0);
		message_dialog("Suddenly you hear a loud click, and a part of the east wall moves away.","");	}
	else
		set_state_continue(15);
break;

beginstate 21;
	if (get_flag(2,23) == 0)	{
		spawn_creature(14);
		put_boom_on_char(14,2,0);
		spawn_creature(15);
		put_boom_on_char(15,2,0);
		run_animation_sound(10);
		set_flag(2,23,1);	}
break;

beginstate 22;
	if (get_flag(2,24) == 1)	{
		set_state_continue(25);	}
	else	{
		spawn_creature(16);
		put_boom_on_char(16,2,0);
		run_animation_sound(10);
		pause(20);
		message_dialog("With a great flash of light, the lich guy teleports into the room with a look of fury on his face.  _Hey, you're not supposed to be here!_ he roars, as he starts to conjure a spell.","_Come with me... and I will show you my super eeeevil plan!!_");
		set_terrain(36,25,39);
		teleport_party(37,26,0);
		force_view_center(39,25);
		force_instant_terrain_redraw();
		erase_char(16);
		pause(10);
		spawn_creature(17);
		put_boom_on_char(17,2,0);
		run_animation_sound(10);
		pause(20);
		message_dialog("Lich man reappears before you, this time looking maliciously content.  _I gotcha now,_ he cackles. _I've held you off long enough that I've perfected my spell!  Wanna know what it does?_","_Well too bad,_ he snaps, _'cause I'm gonna show you anyway!_  He starts waving his hands around and chanting.");
		pause(10);
		relocate_character(17,44,25);
		force_instant_terrain_redraw();
		pause(15);
		set_character_pose(17,1);
		force_instant_terrain_redraw();
		pause(3);
		put_boom_on_char(17,4,0);
		run_animation_sound(54);
		pause(5);
		set_terrain(38,19,0);
		set_terrain(38,31,0);
		activate_hidden_group(5);
		put_effect_on_char(18,2,4,0);
		put_effect_on_char(19,2,4,0);
		put_effect_on_char(20,2,4,0);
		put_effect_on_char(21,2,4,0);
		put_effect_on_char(22,2,4,0);
		put_effect_on_char(23,2,4,0);
		put_effect_on_char(24,2,4,0);
		put_effect_on_char(25,2,4,0);
		put_effect_on_char(26,2,4,0);
		put_effect_on_char(27,2,4,0);
		put_effect_on_char(28,2,4,0);
		put_effect_on_char(29,2,4,0);
		run_animation();
		pause(2);
		set_character_pose(35,2);
		force_instant_terrain_redraw();
		pause(20);
		reset_dialog();
		add_dialog_str(0,"You watch in horror as skeletal warriors climb from the skull piles, looking at you from empty eyesockets.  You can feel their hatred as it surrounds and oppresses you.  The lich cackles uproariously as if this is the funniest thing he's ever seen.",0);
		add_dialog_str(1,"_You see,_ he says, still laughing, _I've been tinkering with this spell for a long time now, and it's finally ready to be used against you puny mortals!  And it makes lotsa skeletons,_ he says proudly.",0);
		add_dialog_str(2,"_Soon I'll make even MORE lotsa skeletons, and what then, mortal, what then?!?_  He clenches his bony fist as he says this, and the skeletons ready their blades.  _Your stupid little world will be crushed to smithereens!!_",0);
		add_dialog_str(3,"_SMITHEREENS, I tell you, SMITHEREENS!!_ he yells, laughing insanely.  Suddenly he stops and realizes it's time to make his exit.  He takes a few steps back and disappears, leaving you to deal with lotsa evil minions.",0);
		add_dialog_choice(0,"OK.");
		run_dialog(1);
		erase_char(17);
		set_flag(2,24,1);
		lastskelspawn = 0;		}
break;

beginstate 23;
	if ((get_flag(2,24) == 1) && (get_flag(2,26) == 0))	{
		set_terrain(44,14,0);
		spawn_creature(30);
		spawn_creature(31);
		spawn_creature(32);
		spawn_creature(33);
		put_effect_on_char(30,2,4,0);
		put_effect_on_char(31,2,4,0);
		put_effect_on_char(32,2,4,0);
		put_effect_on_char(33,2,4,0);
		run_animation();
		set_flag(2,26,1);	}
break;

beginstate 24;
	if (get_flag(2,26) == 1)	{
		spawn_creature(34);
		spawn_creature(35);
		set_terrain(40,7,0);
		set_terrain(40,3,0);
		put_effect_on_char(34,2,4,0);
		put_effect_on_char(35,2,4,0);
		run_animation();
		set_flag(2,26,2);	}
break;

beginstate 25;
	if (get_flag(2,25) == 0)	{
	reset_dialog();
	add_dialog_str(0,"On a tall glowing pedestal lays a small glistening orb.  You can feel it radiating a powerful energy from within.  What do you want to do?",0);
	add_dialog_choice(0,"Leave it alone.");
	add_dialog_choice(1,"Destroy it.");
	choice = run_dialog(1);
	if (choice == 1)	{
		block_entry(1);	}
	else	{
		message_dialog("You smash the orb with all the force you can muster, and suddenly a blinding light fills the room as it shatters to a million pieces.  A phantom image of the lich passes by you for an moment, and then it is gone.","The dust settles and the light dims to normal, when suddenly the ground is shaken by a devastating quake.  The place seems to be caving in on itself!  You'd better find a way to escape and fast.");
		set_flag(2,25,1);
		set_terrain(30,5,137);
		set_terrain(41,29,138);
		set_terrain(44,32,138);
		set_terrain(37,27,138);
		set_terrain(39,29,138);
		set_terrain(38,23,138);
		set_terrain(41,20,138);
		set_terrain(42,10,138);
		set_terrain(43,12,138);
		set_terrain(42,15,138);
		set_terrain(38,7,138);
		set_terrain(35,6,125);
		set_terrain(41,31,132);
		set_terrain(41,25,137);		
		timeleft = 70;		}	}
break;

beginstate 26;
	message_dialog("This appears to be the path Raelnis came from.  It is long and winding, and also very dark and close.  You doubt you could fit into some areas.  You decide not to travel any farther into it.","");
	block_entry(1);
break;

beginstate 27;
	reset_dialog();
	add_dialog_str(0,"As you clamber up the steep incline, an explosion shakes the whole mountain.  The earth beneath you begins to buckle and you scramble for the light at the top of the slope.  The rumbling sound all around you is deafening.",0);
	add_dialog_str(1,"You finally stagger out the exit and run away from it, and you turn to see the whole cave collapse upon itself.  You are on a small mountain path, unseen from outside.  With a little hiking, you reach a crossroads where you can travel safely.",0);
	add_dialog_str(2,"Turning back, you recall your friends Raelnis and Banek, but you realize that they have been through much worse.  They no doubt escaped in safety.",0);
	add_dialog_str(3,"Unfortunately you found no vast treasures within the caves as were described, but you have gained much experience and you have cleared the world of another menace.  Overall the quest was a satisfying one.",0);
	add_dialog_str(4,"After your silent reflection you face the path before you and begin your long walk to civilization.  A vast world filled with opportunities for adventurers such as you is waiting, and your spirits soar at the thought.",0);
	add_dialog_str(5,"And so, having completed your quest, you trod off into the mountains, their peaks lit by the bright sunset.",0);
	add_dialog_choice(0,"The end!");
	run_dialog(0);
	end_scenario(1);
break;

beginstate 28;
	if (get_flag(3,6) == 1)	{
		message_dialog("Now that you've met the bizzare lich-like being again, perhaps you'd better inspect these graves.  Hint hint.  Nudge.  Wink.  And so forth.","");
		set_flag(3,6,2);	}
break;
